package com.example.zhanglinan.vrdemo.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.example.zhanglinan.vrdemo.R;
import com.example.zhanglinan.vrdemo.adapter.MyViewPager;
import com.example.zhanglinan.vrdemo.bean.VideoBean;
import com.example.zhanglinan.vrdemo.fragment.HuntFragment;
import com.example.zhanglinan.vrdemo.fragment.PanoFragmnet;
import com.example.zhanglinan.vrdemo.fragment.VideoFragment;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private DrawerLayout dlMain;
    private ActionBarDrawerToggle mToggle;
    private ViewPager mVpMain;
    private TabLayout mTabLayout;
    private NavigationView mNavigationView;
    private Toolbar mToolbar;
    private AppBarLayout mAppBarLayout;
    private ImageView mImageView;
    private TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    private void init() {
        initView();
        initActionBar();
        initViewPager();
        initListView();

        mAppBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                //总高度
                int range = appBarLayout.getTotalScrollRange();
                int alpha = 255 * Math.abs(verticalOffset) / range;
                mToolbar.setBackgroundColor(Color.argb(alpha, 255, 152, 0));
                mTextView.setTextColor(Color.argb(alpha, 75, 225, 16));
            }
        });

        mImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"点击了图片",Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void initView() {
        dlMain = (DrawerLayout) findViewById(R.id.activity_main);
        mVpMain = (ViewPager) findViewById(R.id.vp_main);
        mTabLayout = (TabLayout) findViewById(R.id.tabLayout_main);
        mNavigationView = (NavigationView) findViewById(R.id.nv_main);
        mToolbar = (Toolbar) findViewById(R.id.tb_main);
        mAppBarLayout = (AppBarLayout) findViewById(R.id.abl_main);
        mImageView = (ImageView) findViewById(R.id.iv_main);
        mTextView = (TextView) findViewById(R.id.tv_main);
    }

    private void initActionBar() {
        mToggle = new ActionBarDrawerToggle(this, dlMain, mToolbar, R.string.open, R.string.close);
        mToggle.syncState();
        dlMain.addDrawerListener(mToggle);
    }

    private void initViewPager() {
        mTabLayout.addTab(mTabLayout.newTab());
        mTabLayout.addTab(mTabLayout.newTab());
        mTabLayout.addTab(mTabLayout.newTab());
        final List<Fragment> fragments = new ArrayList<>();

        fragments.add(new PanoFragmnet());
        OkGo.get("http://api.youkes.com:8081/api/video/query")
                .tag(this)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        try {
                            JSONObject obj = new JSONObject(s);
                            String content = obj.getString("content");
                            List<VideoBean> list = JSON.parseArray(content, VideoBean.class);
                            fragments.add(new VideoFragment(list));
                            fragments.add(new HuntFragment());
                            mVpMain.setAdapter(new MyViewPager(getSupportFragmentManager(), fragments));
                            //tablayout和viewpager进行绑定
                            mTabLayout.setupWithViewPager(mVpMain);
                            //设置tab的标题 setupwith会清空tab
                            mTabLayout.getTabAt(0).setText("全景图");
                            mTabLayout.getTabAt(1).setText("全景视频");
                            mTabLayout.getTabAt(2).setText("寻宝游戏");
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    private void initListView() {
        //使用6.0控件
        mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                item.setChecked(true);
                switch (item.getItemId()) {
                    case R.id.menu_pano:
                        mVpMain.setCurrentItem(0);
                        break;
                    case R.id.menu_video:
                        mVpMain.setCurrentItem(1);
                        break;
                    case R.id.menu_game:
                        mVpMain.setCurrentItem(2);
                        break;
                    case R.id.menu_more:
                        Toast.makeText(MainActivity.this, "敬请期待", Toast.LENGTH_SHORT).show();
                        break;
                }
                dlMain.closeDrawers();
                return true;
            }
        });
    }
}
